﻿using FastCollections;
using Lockstep.UI;
using UnityEngine;

namespace Lockstep
{
    public class StatsManager : BehaviourHelper
    {
        [SerializeField]
        public GameObject statsPrefab;
        [SerializeField]
        public GameObject attachCanvas;

        public static StatsManager Instance { get; private set; }
        static readonly FastBucket<StatsBar> activeStatsBar = new FastBucket<StatsBar>();
   
        protected override void OnInitialize()
        {
            if(Instance == null)
            {
                Instance = this;
            }
        }
        protected override void OnGameStart() { }

        public static void CreateStatsBar(LSAgent agent)
        {
            GameObject newStatsBarObj = Instantiate(Instance.statsPrefab, Instance.attachCanvas.transform);
            StatsBar statsBarCom = newStatsBarObj.GetComponent<StatsBar>();
            statsBarCom.Setup(agent);
            activeStatsBar.Add(statsBarCom);
        }

        public static void DestroyStatsBar(StatsBar statsBar)
        {
            activeStatsBar.Remove(statsBar);
            //进行cache或者直接删除
            Destroy(statsBar.gameObject);
        }

        protected override void OnVisualize()
        {
            for (int i = 0; i < activeStatsBar.PeakCount; i++)
            {
                if (activeStatsBar.arrayAllocation[i])
                {
                    StatsBar statsBar = activeStatsBar[i];
                    statsBar.Visualize();
                }
            }
        }
    }
}